﻿#include "WebEngineView.h"
#include <QMessageBox>
#include <QNetworkProxyFactory>

WebEngineView* WebEngineView::m_instance = nullptr;

WebEngineView::WebEngineView(QWidget* parent)
	:QWebEngineView(parent)
{
	QNetworkProxyFactory::setUseSystemConfiguration(false);
	m_pWebChannel = new QWebChannel(this);
	//注册一个HostApplication对象，html端通过此名称向qt端发送消息
	m_pWebChannel->registerObject("HostApplication", this);
	this->page()->setWebChannel(m_pWebChannel);
	m_instance = this;
}

WebEngineView::~WebEngineView()
{
	m_pWebChannel->deregisterObject(this);
}

void WebEngineView::openDevTool()
{
	if (m_devTool) {
		delete m_devTool;
		m_devTool = nullptr;
	}
	m_devTool = new QWebEngineView();
	this->page()->setDevToolsPage(m_devTool->page());
	m_devTool->show();
}

QWebEngineView* WebEngineView::instance()
{
	return m_instance;
}

// 当需要打开新的窗口时候调用，url会被qt自动设置
QWebEngineView* WebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
	Q_UNUSED(type);
	return this;
}


void WebEngineView::recieveJsMessage(const QString& jsMsg)
{
	QMessageBox::information(this, "title", "Qt接受JS消息:" + jsMsg);
}